package com.carsa.blackberry.domain.services.webServices;

import java.rmi.RemoteException;
import java.util.Vector;

import javax.xml.rpc.Stub;

import com.carsa.blackberry.MainKeys;
import com.carsa.blackberry.domain.services.webServices.impl.ZSdareAre.ZWSDARE_ARE_Stub;
import com.carsa.blackberry.domain.services.webServices.impl.ZSdareAre.ZsdareAre;
import com.carsa.blackberry.domain.services.webServices.impl.ZSdareAre.ZsdareAret;
import com.carsa.blackberry.util.Logger;
import com.carsa.blackberry.util.XMLHandler;

/**
 * @author Nahuel Barrios.
 * @since 11/08/2010.
 * @version 1.0
 */
public class ZSdareAre_Service extends ZWSDARE_ARE_Stub implements WebService {

    public Object invokeWebService(Object parameters) {
	Object[] parametersArray = null;

	if (parameters.getClass().equals(Vector.class))
	    parametersArray = this.parseParameters((Vector) parameters);
	else if (parameters.getClass().equals(String.class))
	    parametersArray = this.parseParameters((String) parameters);

	return this.invokeWebService(parametersArray);
    }

    public Object invokeWebService() {
	return null;
    }

    public Object invokeWebService(Object[] parametersArray) {
	ZWSDARE_ARE_Stub ws = new ZWSDARE_ARE_Stub();

	ws._setProperty(Stub.USERNAME_PROPERTY, MainKeys.WEB_SERVICES_USERNAME);
	ws._setProperty(Stub.PASSWORD_PROPERTY, MainKeys.WEB_SERVICES_PASSWORD);

	try {
	    return ws.ZSdareAre((ZsdareAret) parametersArray[0]);
	}
	catch (RemoteException e) {
	    Logger.logError(e);
	}
	catch (Exception anException) {
	    Logger.logError(Logger.getLocationMessageFor(this, "invokeWebService()")
		    + " intentando ejecutar el Web Service", anException);
	}
	return null;
    }

    public Object[] parseParameters(Vector parameters) {
	ZsdareAre are = (ZsdareAre) parameters.elementAt(0);

	if (are.getGrund() == null)
	    are.setGrund("");

	if (are.getSubmo() == null)
	    are.setSubmo("");

	ZsdareAret areType = new ZsdareAret();
	areType.setItem(new ZsdareAre[] { are });

	return new Object[] { areType };
    }

    public Object[] parseParameters(String xmlParameters) {
	XMLHandler parameters = new XMLHandler(xmlParameters);
	parameters.removeRootTag();

	// En este array van a ir todos los parametros que finalmente paso al metodo.
	Object[] returnObject = new Object[1];

	// Parseo el unico elemento complejo que tiene este Web Service.
	Vector items = new Vector();
	XMLHandler xmlItem = new XMLHandler(parameters.getElementValue(_qname_ZSdareAre.getLocalPart()));
	// int elementsCount = xmlItem.getElementCount();
	// for (int elementIdx = 0; elementIdx < elementsCount; elementIdx++) {
	// XMLHandler complexTypeHandler = new XMLHandler(xmlItem.getFirstElementValueOverIteration());

	ZsdareAre anItem = new ZsdareAre();
	anItem.setAyuda(xmlItem.getElementValue(_qname_Ayuda.getLocalPart()));
	anItem.setFeare(xmlItem.getElementValue(_qname_Feare.getLocalPart()));
	anItem.setGrund(xmlItem.getElementValue(_qname_Grund.getLocalPart()));
	anItem.setHoare(xmlItem.getElementValue(_qname_Hoare.getLocalPart()));
	anItem.setLfdif(Double.parseDouble(xmlItem.getElementValue(_qname_Lfdif.getLocalPart())));
	anItem.setLfimg(Double.parseDouble(xmlItem.getElementValue(_qname_Lfimg.getLocalPart())));
	anItem.setPosnr(xmlItem.getElementValue(_qname_Posnr.getLocalPart()));
	anItem.setSammg(xmlItem.getElementValue(_qname_Sammg.getLocalPart()));
	anItem.setSubmo(xmlItem.getElementValue(_qname_Submo.getLocalPart()));
	anItem.setVbeln(xmlItem.getElementValue(_qname_Vbeln.getLocalPart()));

	items.addElement(anItem);
	// }
	ZsdareAre[] itemsArray = new ZsdareAre[items.size()];
	items.copyInto(itemsArray);
	ZsdareAret aList = new ZsdareAret(itemsArray);

	// Agrego el elemento.
	returnObject[0] = aList;

	return returnObject;
    }

    public String getName() {
	return MainKeys.WS_NAME_ZSDARE_ARE;
    }

}
